package com.schoolgard.action.device;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.SchoolDataService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.MsgReceiver;
import com.schoolgard.entitybean.SchoolData;
import com.supersystem.utils.JSONUtils;

public class SchoolDataAction extends BaseAction {
	
	private SchoolDataService schoolDataService;


	public void setSchoolDataService(SchoolDataService schoolDataService) {
		this.schoolDataService = schoolDataService;
	}

	private SchoolData schoolData;
	
	private MsgReceiver msgReceiver;

	public void findAll() {
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数

		int start = Integer.parseInt(startStr);
		int limit = Integer.parseInt(limitStr);
		if (limit == 0) {
			limit = Integer.MAX_VALUE;
		}
		GridDataModel<SchoolData> page = schoolDataService.find4Page(schoolData, start, limit);
		String jsonStr = JSONObject.fromObject(page).toString();
		writeToResponse(jsonStr);
	}
	
	public void findReceiverAll() {
		String schoolId = request.getParameter("schoolId");
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数

		int start = Integer.parseInt(startStr);
		int limit = Integer.parseInt(limitStr);
		if (limit == 0) {
			limit = Integer.MAX_VALUE;
		}
		GridDataModel<MsgReceiver> page = schoolDataService.findReceiverBySchoolId(Long.valueOf(schoolId), start, limit);
		String jsonStr = JSONObject.fromObject(page).toString();
		writeToResponse(jsonStr);
	}

	public void saveReceiver() {
		Long id = msgReceiver.getId();
		MsgReceiver receiver = null;
		if (id == null || id == 0) {
			receiver = new MsgReceiver(); // 新增
		} else {
			receiver = schoolDataService.findById(id);
		}
		receiver.setName(msgReceiver.getName());
		receiver.setPhone(msgReceiver.getPhone());
		receiver.setSchoolId(msgReceiver.getSchoolId());
		receiver.setStatus(1);
		receiver.setType(1);
		schoolDataService.addReceiver(receiver);
	}

	public void delete() {
		String id = request.getParameter("deleteId");
		if (StringUtils.isBlank(id)) {
			writeToResponse("0");
			return;
		}
		schoolDataService.deleteReceiver(Long.valueOf(id));
		writeToResponse("1");
	}

	public void findByID() {
		String id = request.getParameter("strategyId");
		if (StringUtils.isEmpty(id)) {
			return;
		}
		MsgReceiver handle = schoolDataService.findById(Long.valueOf(id));
		writeToResponse(JSONUtils.toJSONString(handle));
	}

	public void updateStatus(){
		String id = request.getParameter("selectedId");
		String status = request.getParameter("status");
		String schoolId = request.getParameter("schoolId");
		String type = request.getParameter("type");
		if (StringUtils.isEmpty(id)) {
			return;
		}
		if("0".equals(status) && "0".equals(type)){
			int count = schoolDataService.findSysReceiverEnable(Long.valueOf(schoolId));
			if(count <= 1) {
				writeToResponse("9");
				return;
			}
		}
		boolean result = schoolDataService.updateStatus(Long.valueOf(id),Integer.valueOf(status));
		if(result) {
			writeToResponse("1");
		}else{
			writeToResponse("0");
		}	
	}
	
	public void updateSchool(){
		try{
			schoolDataService.updateSchool();
			writeToResponse("1");
		}catch(Exception e) {
			e.printStackTrace();
			writeToResponse("0");
		}
	}
	
	public void receiverUpdate(){
		String schoolId = request.getParameter("schoolId");
		if(schoolId == null) {
			writeToResponse("0");
			return;
		}
		try{
			schoolDataService.findRemoteDate(Long.valueOf(schoolId));
			writeToResponse("1");
		}catch(Exception e){
			e.printStackTrace();
			writeToResponse("0");
		}
	}
	
	public MsgReceiver getMsgReceiver() {
		return msgReceiver;
	}

	public void setMsgReceiver(MsgReceiver msgReceiver) {
		this.msgReceiver = msgReceiver;
	}

	public SchoolData getSchoolData() {
		return schoolData;
	}

	public void setSchoolData(SchoolData schoolData) {
		this.schoolData = schoolData;
	}
}
